package com.ykh.mybbsdemo.util;

import com.alibaba.fastjson.JSON;
import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.BatchStatus;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.storage.model.FileInfo;
import com.qiniu.util.Auth;
import com.ykh.mybbsdemo.config.QiNiuConfig;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

@Component

public class QiniuUtils {
    private static String accessKey = QiNiuConfig.getInstance().getAccessKey();
    private static final String secretKey = QiNiuConfig.getInstance().getSecretKey();
    private static final String bucket = QiNiuConfig.getInstance().getBucket();
    private static final Region region = QiNiuConfig.getInstance().getRegion();
    public  String upload(MultipartFile file,String fileName){
        //构造一个带指定 Region 对象的配置类
        Configuration cfg = new Configuration(region);
        //...其他参数参考类注释
        UploadManager uploadManager = new UploadManager(cfg);
        //...生成上传凭证，然后准备上传

        //默认不指定key的情况下，以文件内容的hash值作为文件名
        try {
            byte[] uploadBytes = file.getBytes();
            Auth auth = Auth.create(accessKey, secretKey);
            String upToken = auth.uploadToken(bucket);
                Response response = uploadManager.put(uploadBytes, fileName, upToken);
                //解析上传成功的结果
                DefaultPutRet putRet = JSON.parseObject(response.bodyString(), DefaultPutRet.class);
//                String key = putRet.key;
//                System.out.println("=============================");
//                System.out.println(key);
//                System.out.println("=============================");
            return putRet.key;
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        return null;
    }

    /** * 获取所有的bucket */
    public static String[] getBucketsInfo() {
        try {
            BucketManager bucketManager = getBucketManager();
            //获取所有的bucket信息
            String[] buckets = bucketManager.buckets();
            return buckets;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /** * 获取文件访问地址 * * @param fileName 文件云端存储的名称 * @return * @throws UnsupportedEncodingException */
    public String fileUrl(String fileName) throws UnsupportedEncodingException {
        String encodedFileName = URLEncoder.encode(fileName, "utf-8");
        String publicUrl = String.format("%s/%s", bucket, encodedFileName);
        Auth auth = getAuth();
        long expireInSeconds = QiNiuConfig.getInstance().getExpireInSeconds();
        if (-1 == expireInSeconds) {
            return auth.privateDownloadUrl(publicUrl);
        }
        return auth.privateDownloadUrl(publicUrl, expireInSeconds);
    }

    /** * 获取bucket里面所有文件的信息 */
    public static FileInfo[] getFileInfo() {
        try {
            BucketManager bucketManager = getBucketManager();
            //文件名前缀
            String prefix = "";
            //每次迭代的长度限制，最大1000，推荐值 1000
            int limit = 1000;
            //指定目录分隔符，列出所有公共前缀（模拟列出目录效果）。缺省值为空字符串
            String delimiter = "";
            //列举空间文件列表
            BucketManager.FileListIterator fileListIterator = bucketManager.createFileListIterator(bucket, prefix, limit, delimiter);
            while (fileListIterator.hasNext()) {
                //处理获取的file list结果
                FileInfo[] items = fileListIterator.next();
                return items;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    /** * 删除多个文件 * * @param keys 文件名称数组 * @return */
    public  Map<String, String> deletes(String... keys) {
        Map<String, String> map = new HashMap<>();
        try {
            //设定删除的数据
            BucketManager.BatchOperations batchOperations = new BucketManager.BatchOperations();
            batchOperations.addDeleteOp(bucket, keys);
            BucketManager bucketManager = getBucketManager();
            //发送请求
            Response response = bucketManager.batch(batchOperations);
            //返回数据
            BatchStatus[] batchStatusList = response.jsonToObject(BatchStatus[].class);
            for (int i = 0; i < keys.length; i++) {
                BatchStatus status = batchStatusList[i];
                String key = keys[i];
                if (status.code == 200) {
                    map.put(key, "delete success");
                } else {
                    map.put(key, "delete failed");
                }
            }
        } catch (Exception e) {
        }
        return map;
    }
    /** * 删除bucket中的文件名称 * * @param key 文件名称 * @return */
    public  boolean delete(String key) {
        try {
            BucketManager mg = getBucketManager();
            mg.delete(bucket, key);
            return true;
        } catch (Exception e) {
            return false;
        }
    }
//    /** * 获取上传凭证 * * @return */
//    public  String getUpToken() {
//        StringMap policy = new StringMap();
//        policy.put("file_size_limit", fileSizeLimit);
//        return getAuth().uploadToken(bucket, accessKey, 10000L, policy);
//    }

    public  Auth getAuth() {
        Auth auth = Auth.create(accessKey, secretKey);
        return auth;
    }
    /** * 获取上传管理器 * * @return */
    public static UploadManager getUploadManager() {
        //构造一个带指定Zone对象的配置类
        Configuration cfg = new Configuration(region);
        //...其他参数参考类注释
        UploadManager uploadManager = new UploadManager(cfg);
        return uploadManager;
    }
    /** * 获取Bucket的管理对象 * * @return */
    public static BucketManager getBucketManager() {
        Configuration cfg = new Configuration(region);
        Auth auth = Auth.create(accessKey, secretKey);
        BucketManager bucketManager = new BucketManager(auth, cfg);
        return bucketManager;
    }
}
